package com.wendy.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author zhuwending
 */
@Slf4j
public class HttpClientUtils {

    public static RequestConfig config() {
        return RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
    }

    public static JSONObject httpPost(String url, String param) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonObject = null;
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(config());
        if (StringUtils.isNotBlank(param)) {
            StringEntity entity = new StringEntity(param, StandardCharsets.UTF_8);
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/x-www-form-urlencoded");
            httpPost.setEntity(entity);
        }
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                jsonObject = JSONObject.parseObject(result);
            } else {
                log.error("请求提交失败" + url);
            }
        } catch (IOException e) {
            log.error("请求提交失败！失败原因：" + e.getMessage() + "失败请求url：" + url, e);
        }
        return jsonObject;
    }

    public static JSONObject httpPost(String url, JSONObject param) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject result = null;
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(config());
        try {
            if (null != param) {
                StringEntity entity = new StringEntity(param.toString(), StandardCharsets.UTF_8);
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
            }
            CloseableHttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String resultStr;
                try {
                    resultStr = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                    result = JSONObject.parseObject(resultStr);
                } catch (Exception e) {
                    log.error("请求提交失败！失败原因：" + e.getMessage() + "失败请求url：" + url, e);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpPost.releaseConnection();
        }
        return result;
    }

    public static JSONObject httpGet(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonObject = null;
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(config());
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                jsonObject = JSONObject.parseObject(result);
            }
        } catch (IOException e) {
            log.error("请求提交失败！失败原因：" + e.getMessage() + "失败请求url：" + url, e);
        } finally {
            httpGet.releaseConnection();
        }
        return jsonObject;
    }
}
